/**
 * @file usr_led.c
 * @author zcj (605607217@qq.com)
 * @brief
 * @version ecu-中控
 * @date 2023-11-06
 *
 * @copyright Copyright (c) 2023
 *
 */

#include "main.h"

/*==================================== work led setting =======================*/
/**
 * 任务
 */
luat_rtos_task_handle gsm_led_task_handle;
luat_rtos_task_handle gnss_led_task_handle;
/**
 * 工作结构体定义
 */
WORK_LED_CTRL_T gnss_work_led = {0};
WORK_LED_CTRL_T gsm_work_led = {0};
/**
 * 参数宏定义
 */
#define MAX_DATA_MODE_COUNT (5)
#define MIN_DATA_MODE_COUNT (0)
#define INIT_FLASH_TIME_MS (200)
#define DATA_FLASH_TIME_MS (500)


/**
 * 前向声明
 */
static WORK_LED_STATE_M work_led_state_switch(WORK_LED_CTRL_T *led);
static WORK_LED_MODE_M get_work_led_mode(WORK_LED_CTRL_T *led);
static uint8_t get_data_mode_count(WORK_LED_CTRL_T *led);
static void data_mode_count_dec(WORK_LED_CTRL_T *led);

/**
 * @brief 工作灯模式切换
 *
 * @param led
 * @param staus
 * @author zcj (605607217@qq.com)
 * @date 2023-11-06
 */
void work_led_mode_switch(WORK_LED_CTRL_T *led, WORK_LED_MODE_M staus)
{
    luat_rtos_mutex_lock(led->mutex, LUAT_WAIT_FOREVER);
    led->mode = staus;
    luat_rtos_mutex_unlock(led->mutex);
}

/**
 * @brief 工作灯状态切换
 *
 * @param led
 * @return WORK_LED_STATE_M
 * @author zcj (605607217@qq.com)
 * @date 2023-11-06
 */
static WORK_LED_STATE_M work_led_state_switch(WORK_LED_CTRL_T *led)
{
    luat_rtos_mutex_lock(led->mutex, LUAT_WAIT_FOREVER);
    led->status = !led->status;
    WORK_LED_STATE_M state = led->status;
    luat_rtos_mutex_unlock(led->mutex);
    return state;
}

/**
 * @brief Get the work led mode object
 *
 * @param led
 * @return WORK_LED_MODE_M
 * @author zcj (605607217@qq.com)
 * @date 2023-11-06
 */
static WORK_LED_MODE_M get_work_led_mode(WORK_LED_CTRL_T *led)
{
    luat_rtos_mutex_lock(led->mutex, LUAT_WAIT_FOREVER);
    WORK_LED_MODE_M mode = led->mode;
    luat_rtos_mutex_unlock(led->mutex);
    return mode;
}

/**
 * @brief 数据模式闪烁计数+1
 *
 * @param led
 * @author zcj (605607217@qq.com)
 * @date 2023-11-06
 */
void data_mode_count_add(WORK_LED_CTRL_T *led)
{
    if (get_work_led_mode(led) == MODE_DATA)
    {
        if (get_data_mode_count(led) < MAX_DATA_MODE_COUNT)
        {
            luat_rtos_mutex_lock(led->mutex, LUAT_WAIT_FOREVER);
            led->count++;
            luat_rtos_mutex_unlock(led->mutex);
        }
    }
}

/**
 * @brief 数据模式闪烁计数-1
 *
 * @param led
 * @author zcj (605607217@qq.com)
 * @date 2023-11-06
 */
static void data_mode_count_dec(WORK_LED_CTRL_T *led)
{
    if (get_work_led_mode(led) == MODE_DATA)
    {
        if (get_data_mode_count(led) > MIN_DATA_MODE_COUNT)
        {
            luat_rtos_mutex_lock(led->mutex, LUAT_WAIT_FOREVER);
            led->count--;
            luat_rtos_mutex_unlock(led->mutex);
        }
    }
}

/**
 * @brief Get the data mode count object
 *
 * @param led
 * @return uint8_t
 * @author zcj (605607217@qq.com)
 * @date 2023-11-06
 */
static uint8_t get_data_mode_count(WORK_LED_CTRL_T *led)
{
    luat_rtos_mutex_lock(led->mutex, LUAT_WAIT_FOREVER);
    uint8_t count = led->count;
    luat_rtos_mutex_unlock(led->mutex);
    return count;
}

/**
 * 工作灯任务
 */
static void gnss_led_task(void *param)
{
    work_led_mode_switch(&gnss_work_led, MODE_INIT);//切换到初始化模式
    luat_rtos_mutex_create(&gnss_work_led.mutex);
    while (1)
    {
        deisplay((get_work_led_mode(&gnss_work_led)),GNSS_LED);            
    }
}
static void gsm_led_task(void *param)
{
    work_led_mode_switch(&gsm_work_led, MODE_INIT);//切换到初始化模式
    luat_rtos_mutex_create(&gsm_work_led.mutex);
    while (1)
    {
        deisplay((get_work_led_mode(&gsm_work_led)),GSM_LED);
    }
}
void deisplay(WORK_LED_MODE_M mode,uint8_t data_mode)
{

     switch (mode)
        {
        // case MODE_ERROR:
        // switch (data_mode)
        // {
        // case GNSS_LED:/* constant-expression */
        //     /* code */
        //     gnss_led_ctrl(work_led_state_switch(&gnss_work_led));//亮
        //     usr_delay_ms(500);
        //     gnss_led_ctrl(work_led_state_switch(&gnss_work_led));//灭
        //     usr_delay_ms(1000);
        //     break;
        // case GSM_LED:
        //     net_led_ctrl(work_led_state_switch(&gsm_work_led));//亮
        //     usr_delay_ms(500);
        //     net_led_ctrl(work_led_state_switch(&gsm_work_led));//灭
        //     usr_delay_ms(1000);
        //     break;
        // default:
        //     break;
        // }          
        // break;
        case MODE_INIT:
            // 持续闪烁
             switch (data_mode)
        {
        case GNSS_LED:/* constant-expression */
            /* code */
            gnss_led_ctrl(work_led_state_switch(&gnss_work_led));//亮
            usr_delay_ms(INIT_FLASH_TIME_MS);
            gnss_led_ctrl(work_led_state_switch(&gnss_work_led));//灭
            usr_delay_ms(INIT_FLASH_TIME_MS);
            break;
        case GSM_LED:
            net_led_ctrl(work_led_state_switch(&gsm_work_led));//亮
            usr_delay_ms(INIT_FLASH_TIME_MS);
            net_led_ctrl(work_led_state_switch(&gsm_work_led));//灭
            usr_delay_ms(INIT_FLASH_TIME_MS);
            break;
        default:
            break;
        }          
        break;
        case MODE_DATA:
            switch (data_mode)
            {
            case GNSS_LED:/* constant-expression */
                /* code */
            // 每收发一次消息闪烁一次
                while (get_data_mode_count(&gnss_work_led))
                {
                    gnss_led_ctrl(work_led_state_switch(&gnss_work_led));
                    usr_delay_ms(DATA_FLASH_TIME_MS);
                    gnss_led_ctrl(work_led_state_switch(&gnss_work_led));
                    usr_delay_ms(DATA_FLASH_TIME_MS);
                    data_mode_count_dec(&gnss_work_led);
                }
                break;
            case GSM_LED:
                while (get_data_mode_count(&gsm_work_led))
                {
                    net_led_ctrl(work_led_state_switch(&gsm_work_led));
                    usr_delay_ms(DATA_FLASH_TIME_MS);
                    net_led_ctrl(work_led_state_switch(&gsm_work_led));
                    usr_delay_ms(DATA_FLASH_TIME_MS);
                    data_mode_count_dec(&gsm_work_led);
                }
                break;
            default:
                break;
            }              
            //usr_delay_ms(200);
            break;
        // case MODE_SLEEP:
        // switch (data_mode)
        // {
        // case GNSS_LED:/* constant-expression */
        //     /* code */
        //     gnss_led_ctrl(0);//亮
        //     break;
        // case GSM_LED:
        //     net_led_ctrl(0);//亮
        //     break;
        // default:
        //     break;
        // }          
        // usr_delay_ms(1000);
        // break;
        default:
            break;
        }
}
/**
 * 工作灯任务初始化
 */
void work_led_init(void)
{
    //hard_ware_init();
    luat_rtos_task_create(&gnss_led_task_handle, 1024 * 2, 8, "gnss_led_task", gnss_led_task, NULL, NULL);
    luat_rtos_task_create(&gsm_led_task_handle, 1024 * 2, 8, "gsm_led_task", gsm_led_task, NULL, NULL);
}

